package com.imooc.service.impl;

import com.imooc.common.constants.ResultCodeConstants;
import com.imooc.common.utils.ExceptionProcessorUtils;
import com.imooc.converter.CarouselConverter;
import com.imooc.dto.index.CarouselDto;
import com.imooc.dto.index.CarouselListResponse;
import com.imooc.mapper.CarouselMapper;
import com.imooc.pojo.Carousel;
import com.imooc.service.ICarouselService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: CarouselServiceImpl
 * @Description: 轮播图Service 实现
 * @author: limingxing
 * @Date: 2019-12-24 16:02
 */
@Slf4j
@Service
public class CarouselServiceImpl implements ICarouselService {

    @Autowired
    private CarouselMapper carouselMapper;
    @Autowired
    private CarouselConverter carouselConverter;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CarouselListResponse queryAll(Integer isShow) {
        log.info("Begin CarouselServiceImpl.queryAll  isShow: [{}]", isShow);
        CarouselListResponse response = new CarouselListResponse();
        Example example = new Example(Carousel.class);
        try {
            example.orderBy("sort").desc();
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("isShow", isShow);
            List<Carousel> carouselList = carouselMapper.selectByExample(example);
            List<CarouselDto> list = carouselConverter.carousel2List(carouselList);
            response.setCarouselDtoList(list);
        }catch (Exception e){
            log.error("CarouselServiceImpl.queryAll Occur Exception : [{}]" , e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        response.setCode(ResultCodeConstants.SUCCESS.getCode());
        response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        return response;
    }
}
